

function getRequst(data,url){
	return new Promise((resolve,reject)=>{
		uni.getStorage({
			key: 'token',
			success:  (res)=> {
				uni.request({
				    url: uni.$apis+url,
				    data: data,
				    header: {
				        'token': res.data
				    },
					method:'POST',
				    success: (response) => {
						if(response.data.code!=1){
							let noTipArr  =['免费解锁次数不足，请购买会员查看']
							if(response.data.code==2){
								aginAuth().then((token_)=>{
									uni.request({
									    url: uni.$apis+url,
									    data: data,
									    header: {
									        'token': token_
									    },
										method:'POST',
										success: (res2) => {
											resolve(res2.data)
										}
									})	
								})
							}else if(!noTipArr.includes(response.data.msg)){
								uni.showToast({
									title: response.data.msg,
									icon: 'none'
								});
								resolve(response.data)
							}
						}else{
							resolve(response.data)
						}
						
				    },
					fail:(err)=>{
						reject(err)
					}
				});
			},
			fail:()=>{
				aginAuth().then((token_)=>{
					uni.request({
					    url: uni.$apis+url,
					    data: data,
					    header: {
					        'token': token_
					    },
						method:'POST',
						success: (res2) => {
							resolve(res2.data)
						}
					})	
				})
			}
		});
	})
}
function aginAuth(){
	return new Promise((resolve,reject)=>{
		uni.login({
			provider: 'weixin',
			success:  (loginRes)=> {
				let data = {
					code:loginRes.code,
				}
				uni.request({
				    url: uni.$apis+'/api/Code/wxAuth',
				    data: data,
					method:'POST',
				    success: (response) => {
						uni.setStorage({
							key: 'token',
							data: response.data.data,
							success:(data)=> {
								resolve(response.data.data)
							}
						});
				    },
					fail:(err)=>{
						reject(err)
					}
				});
			}
		})
	})
	
}
function queryAuth(data){
	return new Promise((resolve,reject)=>{
		uni.request({
		    url: uni.$apis+'/api/Code/wxAuth',
		    data: data,
			method:'POST',
		    success: (response) => {
				resolve(response.data)
		    },
			fail:(err)=>{
				reject(err)
			}
		});
	})
}
/* 
注意：小程序已经回收open-data组件
  1.先调起授权弹窗，才能去wx.login获取code
  2.getUserProfile()最新版获取用户信息的接口，此接口不需要在button上加open-type=“getUserInfo的属性，
    此接口会调起弹窗，此接口返回code
最新：此接口作废，因为用来最新的getUserProfile()所以不需要再解密，其次本次版本不需要授权获取信息 
 */


const getUserProfile = (callback) => {
 	return new Promise((resolve, reject) => {
 		const app = this
  		wx.getUserProfile({
  			lang: 'zh_CN',
  			desc: '获取用户相关信息',
  			success(res) {
  				console.log('用户同意了授权')
				console.log(res)
				uni.showLoading({
					title: '加载中'
				});
 				wx.login({
 					success: ress => {
						console.log(ress)
 						uni.hideLoading();
 					}
 				})
  			},
  			fail() {
  				uni.clearStorageSync('userInfo')
  				uni.clearStorageSync('openId')
				uni.hideLoading();
  				console.log('用户拒绝了授权')
  			}
  		})
 		})
  }


export default {
	getRequst,queryAuth,getUserProfile,aginAuth
}